package com.china08.yxyapi.service.updateresource.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.res.YxyResQues;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.repository.mg.YxyResQuesRepository;
import com.china08.yxyapi.service.updateresource.QuesUpdateService;

@Service
public class QuesUpdateServiceImpl implements QuesUpdateService {

    @Autowired
    private YxyQuesKnowRepository yxyKnowledgeRepository;

    @Autowired
    private YxyResQuesRepository yxyResQuesRepository;

    @Autowired
    private ElasticsearchTemplate et;

    @Override
    public void updateQues(List<YxyQuesKnow> knowledges) {
        List<Integer> knowIds = new ArrayList<Integer>();
        for (YxyQuesKnow yxyKnowledge : knowledges) {
            yxyKnowledge.setIsUpdate(0);
            yxyKnowledgeRepository.save(yxyKnowledge);
            knowIds.clear();
            Integer knowId = yxyKnowledge.getId();
            knowIds.add(knowId);
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            boolQueryBuilder.must(new TermsQueryBuilder("knowFullIds", knowIds));

            SearchQuery countQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();
            long total = et.count(countQuery, ElasticYxyYxyQuesResource.class);
            if (total == 0) {
                continue;
            }
            Pageable pageable = new PageRequest(0, Integer.parseInt(total + ""));
            SearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withPageable(pageable)
                    .build();
            List<ElasticYxyYxyQuesResource> elasticYxyYxyQuesResources = et.queryForList(query,
                    ElasticYxyYxyQuesResource.class);
            for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : elasticYxyYxyQuesResources) {
                String quesId = elasticYxyYxyQuesResource.getId();
                List<Integer> quesKnowIds = elasticYxyYxyQuesResource.getKnowledgeIds();
                List<String> quesKnowName = new ArrayList<String>();
                String fullTextParentId = "";
                YxyResQues ques = yxyResQuesRepository.findById(quesId);
                if (ques == null) {
                    continue;
                }
                boolean isFlag = true;
                for (Integer integer : quesKnowIds) {
                    if (integer == null) {
                        isFlag = false;
                        continue;
                    }
                    YxyQuesKnow knowledge = yxyKnowledgeRepository.findById(integer);
                    if (knowledge == null) {
                        isFlag = false;
                        continue;
                    }
                    quesKnowName.add(knowledge.getName());
                    //
                    String fullStr = parentId(integer, "");
                    fullStr = StringUtils.substring(fullStr, 0, fullStr.length() - 1);
                    fullTextParentId = fullTextParentId + fullStr + "|";
                }
                if (!isFlag) {
                    continue;
                }
                fullTextParentId = StringUtils.substring(fullTextParentId, 0, fullTextParentId.length() - 1);
                ques.setKnowFullIds(fullTextParentId);
                ques.setKnowledgeTexts(StringUtils.join(quesKnowName, ","));
                ques.setKnowledgeIds(StringUtils.join(quesKnowIds, ","));
                ques.setSycEsFlag(0);
                yxyResQuesRepository.save(ques);
            }
        }
    }

    public String parentId(Integer knowId, String str) {
        if (knowId.intValue() == 0) { return str; }
        str = str + knowId + ",";
        YxyQuesKnow knowledge = yxyKnowledgeRepository.findById(knowId);
        str = parentId(knowledge.getParentId(), str);
        return str;
    }

}
